package view;

import com.alibaba.fastjson.JSON;
import model.user.Admin;
import model.user.Staff;
import net.ToServer;
import net.passClass.PassData;
import net.passClass.PassStaff;
import util.MD5;
import viewModel.CurrentUser;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.IOException;

public class LoginInterface extends JFrame {
    private final JPanel view = new JPanel();
    private final JPasswordField passwordField = new JPasswordField();


    public LoginInterface() {
        super("东软颐养中心");

        Box ah = Box.createHorizontalBox();
        ah.add(Box.createHorizontalStrut(45));
        Box a = Box.createVerticalBox();
        ah.add(a);
        ah.add(Box.createHorizontalStrut(45));

        //jSplitPane.setLayout(new GridLayout(1,1));

        a.add(Box.createVerticalStrut(130));
        JLabel label = new JLabel();

        JPanel f = new JPanel();
        f.setLayout(new FlowLayout(FlowLayout.CENTER));
        f.add(label);
        a.add(f);

        JPanel b = new JPanel();
        b.setLayout(new FlowLayout(FlowLayout.LEFT));
        JLabel nameLabel = new JLabel("用户名");
        b.add(nameLabel);
        a.add(b);
        JTextField usernameField = new JTextField(12);
        a.add(usernameField);
        JPanel c = new JPanel();
        c.setLayout(new FlowLayout(FlowLayout.LEFT));
        JLabel pwdLabel = new JLabel("密码");
        c.add(pwdLabel);
        a.add(c);
        JPasswordField passwordField = new JPasswordField(12);
        a.add(passwordField);



        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setContentPane(view);
        this.pack();
        final int WIDTH = 320;
        final int HEIGHT = 500;
        this.setBounds((Toolkit.getDefaultToolkit().getScreenSize().width - WIDTH) / 2,
                (Toolkit.getDefaultToolkit().getScreenSize().height - HEIGHT) / 2,
                WIDTH, HEIGHT);
        this.setResizable(false);
        try {
            this.setIconImage(ImageIO.read(new File("images/Transparent_mini_LOGO.png")));
            label.setIcon(new ImageIcon(ImageIO.read(new File("images/Transparent_LOGO.png"))));
        } catch (IOException e) {
            e.printStackTrace();
        }
        JButton loginButton = new JButton("登陆");
        JPanel d = new JPanel();
        d.setLayout(new FlowLayout(FlowLayout.CENTER));
        d.add(loginButton);
        a.add(d);
        this.add(ah);
        this.setVisible(true);
        loginButton.addActionListener(e -> {
            String un = usernameField.getText();
            String pd = String.copyValueOf(passwordField.getPassword());
            PassData receive = ToServer.receive("/user/login","username:"+un+"\npassword:"+ MD5.md5Encryption(pd));
            if (receive.isFlag()){
                if(receive.getMessage().equals("职员登陆成功")){
                    JOptionPane.showMessageDialog(view, receive.getMessage());
                    Staff theStaff = JSON.parseObject(JSON.toJSONString(((PassStaff) ToServer.receive("/user/staff/get", "username:" + un)).getData().get(0)), Staff.class);
                    CurrentUser.user = theStaff;
                    new MainInterface("东软颐养中心    欢迎您，" + theStaff.getName());
                    LoginInterface.this.dispose();
                } else {
                    JOptionPane.showMessageDialog(view, receive.getMessage());
                    CurrentUser.user = Admin.getInstance();
                    new MainInterface("东软颐养中心    欢迎您，管理员");
                    LoginInterface.this.dispose();
                }
            } else {
                JOptionPane.showMessageDialog(view,receive.getMessage());
            }
        });
    }

    public static void main(String[] args) {
        new LoginInterface();
    }
}
